﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="AccountTypeRepository.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the <seealso cref="Hisaab.Data.AccountTypeRepository" /> type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Data
{
    using System;
    using System.Diagnostics.Contracts;
    using System.Linq;
    using System.Linq.Expressions;
    using Entities;
    using Filters;
    using Interfaces;

    using Microsoft.Practices.ServiceLocation;

    /// <summary>
    /// Defines the <seealso cref="Hisaab.Data.AccountTypeRepository"/> type.
    /// </summary>
    public class AccountTypeRepository : IAccountTypeRepository
    {
        /// <summary>
        /// Databse instance to use
        /// </summary>
        private readonly IDatabase _database;

        /// <summary>
        /// Initializes a new instance of the <see cref="AccountTypeRepository"/> class.
        /// </summary>
        public AccountTypeRepository()
            : this(ServiceLocator.Current.GetInstance<IDatabase>())
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AccountTypeRepository"/> class.
        /// </summary>
        /// <param name="database">The database.</param>
        public AccountTypeRepository(IDatabase database)
        {
            Contract.Requires<ArgumentNullException>(database != null);
            _database = database;
        }

        /// <summary>
        /// Gets the filtered.
        /// </summary>
        /// <param name="filter">The filter.</param>
        /// <param name="pageNo">The page no.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <returns>List of filtered items</returns>
        public IQueryable<AccountType> Query(AccountType filter, int? pageNo, int? pageSize)
        {
            return _database.AccountTypes
                .Filter(filter)
                .Page(pageNo, pageSize);
        }

        /// <summary>
        /// Queries the specified filter.
        /// </summary>
        /// <param name="filter">The filter.</param>
        /// <param name="pageNo">The page no.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <returns>List of filtered items</returns>
        public IQueryable<AccountType> Filter(Expression<Func<AccountType, bool>> filter, int? pageNo, int? pageSize)
        {
            return _database.AccountTypes
                .Where(filter)
                .Page(pageNo, pageSize);
        }

        /// <summary>
        /// Finds the by id.
        /// </summary>
        /// <param name="id">The id of entity</param>
        /// <returns>Entity matching by id</returns>
        public AccountType FindById(int id)
        {
            return _database.AccountTypes
                .Filter(new AccountType { Id = id })
                .FirstOrDefault();
        }
    }
}